import About from "@/views/About.vue";
import Config from "@/views/Config.vue";
import Post from "@/views/Post.vue";
import Read from "@/views/Read.vue";
import Thread from "@/views/Thread.vue";
import User from "@/views/User.vue";
import {PostAction} from "@ginstone/nga-api/dist/src/enums/PostAction";
import {createRouter, createWebHistory} from 'vue-router'
import Home from '../views/Home.vue'
import MessageList from "@/views/MessageList.vue";
import MessageRead from "@/views/MessageRead.vue";

/**
 * 路由名称
 */
export const routeNames = {
    // 用户相关
    user: {
        center: "用户中心(id)",
        center2: "用户中心(username)",
        topic: "用户主题",
        replies: "用户回复",
        bookmark: "用户收藏",
    },
    index: "社区",
    config: "配置",
    about: "关于",
    // 私信
    message: {
        list: "私信列表",
        read: "读私信",
    },
    // thread.php 请求
    thread: {
        forumList: "浏览版面主题",
        forumSearch: "搜索版面主题",
        colList: "浏览合集主题",
        colSearch: "搜索合集主题",
        recommend: "精华区",
        favor: "浏览收藏夹",
    }, // read.php 请求
    read: {
        topic: "浏览主题页",
        reply: "查看单一回复",
    },
    // post.php 请求
    post: {
        new: PostAction.NEW,
        quote: PostAction.QUOTE,
        reply: PostAction.REPLY,
        modify: PostAction.MODIFY,
    },
}


const routes = [// 重定向
    {
        path: '/',
        redirect: '/home',
    },
    // 根路由
    {
        path: '/home',
        name: routeNames.index,
        component: Home
    }, {
        path: '/config',
        name: routeNames.config,
        component: Config
    },
    // 关于
    {
        path: '/about', name: routeNames.about, component: About
    },
    // thread.php 请求 start
    {
        path: '/forum/:id',
        name: routeNames.thread.forumList,
        component: Thread,
    }, {
        path: '/forum/:id/recommend',
        name: routeNames.thread.recommend,
        component: Thread,
    }, {
        path: '/forum/:id/:keyword',
        name: routeNames.thread.forumSearch,
        component: Thread,
    }, {
        path: '/col/:id',
        name: routeNames.thread.colList,
        component: Thread,
    }, {
        path: '/col/:id/:keyword',
        name: routeNames.thread.colSearch,
        component: Thread,
    }, {
        path: '/favor/:id',
        name: routeNames.thread.favor,
        component: Thread,
    }, // thread.php 请求 end

    // read.php 请求 start
    {
        path: '/topic/:id',
        name: routeNames.read.topic,
        component: Read,
    }, {
        path: '/reply/:id',
        name: routeNames.read.reply,
        component: Read,
    }, // 用户相关
    {
        path: '/user/:id',
        name: routeNames.user.center,
        component: User,
    }, {
        path: '/username/:name',
        name: routeNames.user.center2,
        component: User,
    }, {
        path: '/user/:id/thread',
        name: routeNames.user.topic,
        component: Thread,
    }, {
        path: '/user/:id/replies',
        name: routeNames.user.replies,
        component: Thread,
    }, // 发帖相关
    {
        path: '/post/new/:fid',
        name: routeNames.post.new,
        component: Post,
    }, {
        path: '/post/quote/:tid/:pid',
        name: routeNames.post.quote,
        component: Post,
    }, {
        path: '/post/reply/:tid/:pid',
        name: routeNames.post.reply,
        component: Post,
    }, {
        path: '/post/modify/:tid/:pid',
        name: routeNames.post.modify,
        component: Post,
    }, {
        path: '/message/list',
        name: routeNames.message.list,
        component: MessageList,
    }, {
        path: '/message/read/:id',
        name: routeNames.message.read,
        component: MessageRead,
    },

]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL), routes
})

router.beforeEach((to, from, next) => {
    // 如果目标路由没有account参数,从from中复制
    if (from.query.hasOwnProperty('account') && !to.query.hasOwnProperty('account')) {
        to.query.account = from.query.account
        next(to)
    } else {
        next()
    }
})

export default router
